<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="钢号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['steelSpecification', validatorRules.steelSpecification]" placeholder="请输入钢号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['standard']" placeholder="请输入规格"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['qualit']" placeholder="请输入材质"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="图号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['figureNumber']" placeholder="请输入图号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="版次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['version']" placeholder="请输入版次"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['location']" placeholder="请输入位置"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单重量(kg)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['singleWeight']" @keyup="singleWeightChange()" placeholder="请输入单个重量(kg)"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="总重(kg)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['totalWeight']" placeholder="请输入总重(kg)" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['count', validatorRules.count]" placeholder="请输入数量" @keyup="countChange"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="面积" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['area']" placeholder="请输入面积"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="工艺路线" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-if="enableChange"
                v-decorator="['flowName',validatorRules.flowName]"
                :trigger-change="true"
                org-fields="id,flow_name"
                dest-fields="flowId,flowName"
                code="tj_flow"
                @callback="popupCallback"/>
              <j-popup
                v-if="!enableChange"
                disabled
                v-decorator="['flowName',validatorRules.flowName]"
                :trigger-change="true"
                org-fields="id,flow_name"
                dest-fields="flowId,flowName"
                code="tj_flow"
                @callback="popupCallback"/>
              <a-input v-decorator="['flowId']" hidden></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "FrameModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        enableChange:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          steelSpecification: {
            rules: [
              { required: true, message: '请输入钢号!'}
            ]
          },
          count: {
            rules: [
              { required: true, message: '请输入数量!'},
            ]
          },
          flowName: {
            rules: [
              { required: true, message: '请选择工艺线路!'},
            ]
          }
        },
        url: {
          add: "/engine/engineering/addFrame",
          edit: "/engine/engineering/editFrame",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.enableChange = this.model.id===undefined || this.model.id===null || this.model.id ===''; //没有工艺线路，属于异常数据，这里部开放修改
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'steelSpecification','standard','qualit','figureNumber','version','location','singleWeight','totalWeight','count','area','remark','flowId','flowName','engineeringId','createBy','createTime','updateBy','updateTime','sysOrgCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['engineeringId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'steelSpecification','standard','qualit','figureNumber','version','location','singleWeight','totalWeight','count','area','remark','flowId','flowName','engineeringId','createBy','createTime','updateBy','updateTime','sysOrgCode'))
      },
      singleWeightChange(){
        let count = this.form.getFieldValue("count")
        let singleWeight = this.form.getFieldValue("singleWeight")
        if (count!== undefined && count !=='' && singleWeight!==undefined && singleWeight !==''){
          this.form.setFieldsValue({"totalWeight":parseInt(count)*parseFloat(singleWeight)})
        }
      },
      countChange(){
        let count = this.form.getFieldValue("count")
        let singleLenght = this.form.getFieldValue("singleLenght")
        if (count!== undefined && count !=='' && singleLenght!==undefined && singleLenght !==''){
          this.form.setFieldsValue({"totalLenght":parseInt(count)*parseFloat(singleLenght)})
        }
        let singleWeight = this.form.getFieldValue("singleWeight")
        if (count!== undefined && count !=='' && singleWeight!==undefined && singleWeight !==''){
          this.form.setFieldsValue({"totalWeight":parseInt(count)*parseFloat(singleWeight)})
        }
      }

    }
  }
</script>
